package com.sky.controller.MultipartFile;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
public class MyMultipartFile {
    @Autowired
    AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    public Result<String> update(MultipartFile file) throws IOException {
        byte[] bytes = file.getBytes();
        String name = file.getOriginalFilename();
        int index = name.lastIndexOf(".");
        String substring = name.substring(index);
        try {
            String upload = aliOssUtil.upload(bytes, UUID.randomUUID().toString() + substring);
            return Result.success(upload);
        } catch (Exception e) {
            throw new RuntimeException("图片上传失败");
        }
    }
}
